# -*- coding: utf-8 -*-
"""
Created on Sat Aug 18 11:10:46 2018

@author: Who

黑部财经
http://www.heibu.com/

"""

import requests
import re
import time
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def get_html_text(url, data):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_Kx(url, data):
    text = get_html_text(url, data)
    if not text:
        return None
    regex = re.compile(r'<div class="date">([\S\s]*?)</div>')
    dates = re.findall(regex, text)
    return dates


def get_dates_Xw(url, data):
    text = get_html_text(url, data)
    if not text:
        return None
    regex = re.compile(r'<div class="art-itemlook">([\s\S]*?)</div>')
    dates = re.findall(regex, text)
    return dates


def count_process_kx():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    while a < 200:
        data = {
            'next': a,
            'table': 'kuaixun',
            'classid': '2',
            'action': 'getmorenews',
            'limit': '20',
            'small_length': '120'
        }
        url = 'http://www.heibu.com/e/action/kuaixun.php'
        a += 1
        print('----黑部财经快讯获取第{}页----'.format(a))
        dates = get_dates_Kx(url, data)
        if not dates:
            continue
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_Xw():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    while a < 200:
        h = time.strftime("%Y-%m-%d %X", time.localtime())[11:13]
        data = {
            'next': a,
            'table': 'news',
            'action': 'getmorenews',
            'limit': '20',
            'small_length': '120'
        }
        url = 'http://www.heibu.com/e/action/getmore.php'
        a += 1
        print('----黑部财经新闻第{}页获取中----'.format(a))
        dates = get_dates_Xw(url, data)
        if not dates:
            continue
        for date in dates:
            if '分钟前' in date.strip():
                d1 += 1
            elif ('小时前' in date.strip()) and (int(date.strip()[:-3]) < int(h)):
                d1 += 1
            elif ('小时前' in date.strip()) and (int(date.strip()[:-3]) >= int(h)):
                d2 += 1
            elif date.strip() == '1天前':
                d2 += 1
            elif date.strip() == '2天前':
                d3 += 1
            else:
                d4 += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '黑部财经'
    t1 = count_process_kx()
    t2 = count_process_Xw()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
